   proc Connect_port { DUT_HOSTNAME USERNAME PASSWORD MODE PORT} {
        global spawn_id TIMEOUT info interrupt
	set password_sent 0
	log_info $info "Connect_port: host:$DUT_HOSTNAME, user:$USERNAME, password:$PASSWORD, mode:$MODE, port:$PORT" 5
	set retry 3
	set lowermode [string tolower $MODE]
	#adding 15 seconds , that's for some PC lose gateway or was configured with wrong dns, then they will need longer time to respond 
	set timeout [expr $TIMEOUT + 15]
	switch $lowermode {
	ssh {
	        spawn $MODE -l $USERNAME $DUT_HOSTNAME;
	}
	telnet {
		if {$PORT != 23} {
			set timeout 3
			set ppid [spawn $MODE $DUT_HOSTNAME $PORT];
	       		log_info $info "ppid : $ppid" 2	
			expect {
				-re "Escape character|Escape Character" {exp_send "\r";}
				-re "refused|connect failed"  {return 2}
				timeout {return 2}
				}
		} else {
			set ppid [spawn $MODE $DUT_HOSTNAME]; 
			log_info $info "ppid: $ppid; spawn_id: $spawn_id; exp_id: expect_out(spawn_id)" 4
		}
	}
	minicom {
		if [ catch {spawn minicom $DUT_HOSTNAME} result ] {
			error "$result"
		}
		set timeout 6
		sleep 5
#when the /dev/ttyS0 was locked, we can delete the LCK file in /var/lock/LCK.* to unlock it
	
	    	for { set i 1 } { $i <= 3 } { incr i } {
			exp_send "\r"
			sleep 1
		}
#this expect sentence was used to clear all garbage which were created by minicom. wait for 6 seconds and timeout, then send out "\r" to start
		expect {
			-re "lock failed" {return 2}
			-re     "Fedora|Linux|Kernel"    { set os "unix";set expect_out(buffer) ""; exp_send "\r";exp_continue;}
			-re     "Microsoft Telnet Service" {set os "windows"; set expect_out(buffer) ""; exp_send "\r";exp_continue;}
			-re "incorrect|minicom|login:|username:|Username:|\\$|\#|\(enable\)|>" {exp_continue }
			timeout {}
			}
		exp_send "\r"
 	}
	}
	set os ""
        while {$interrupt==0} {
	   expect_after { 
		eof {  
			log_info $info "expect_process exit unexpectly" 2
			return 2
		}
	   }
           expect  {
		# console maybe was left with "--More--" output, we should input "q" to quit it first.
		-re "--More--" {exp_send "q"; exp_continue;}
		-re     "Microsoft Telnet Service" {
		# for windows os 
			   set os "windows"
			   set timeout 30 
			   while {$interrupt==0} {
				   expect {
					"yes/no"   {exp_send "yes\n" }
					-re "Denying new connections" {
						log_info $info "Denying new connection" 2;
						return 2}
					-re "failure|Failed"  {
			                        puts "password error, cannot connect to DUT! "
                        			return 2}
					-re "login:" {exp_send "$USERNAME\r"}
					-re "Password:|password" {puts " password is : $PASSWORD"; exp_send "$PASSWORD\r" }
					-re "\\>" {puts "login to $DUT_HOSTNAME\n"; return 1}
					timeout {return 0}
				   }	
			   }
			}
		#for new 5K image telnet to worker
		-re     "Please choose master worker" {exp_send "1\r"}
		-re     "Fedora|Linux|Kernel"    { set os "unix"}
		-re     "\\$"      {
			  if {$os=="unix"} {
				exp_send "su\r"
			  } else {
	                        puts "login to $DUT_HOSTNAME\n";
        	                return 1
		          }
                        }

	        -re	"\#|\(enable\)|\\\\>"      { 
			puts "login to $DUT_HOSTNAME\n"; 
			return 1
			}
                ">"    {
                        exp_send "enable\r"
                        expect  -re "Password:|password" {exp_send "$PASSWORD\n" }
			#fix bug for accessing cisco4006,Apr12_2006
                        exp_continue
                       }
	
		"yes/no"   {exp_send "yes\r" }
                -re "login:|username:|Username:|User:"   {
			if {$password_sent==0} {
				exp_send "$USERNAME\n" 
				set password_sent 1
			}
			}
		-re "Password:|password" {exp_send "$PASSWORD\n";
			expect {
				 -re    "\\$"      {
		                          if {$os=="unix"} {
                		                exp_send "su\r"
						expect -re "Password:|password" {exp_send "$PASSWORD\n"}
		                          } else {
                		                puts "login to $DUT_HOSTNAME\n";
                                		return 1
		                          }
                		        }
		                #for new 5K image telnet to worker
                		-re     "Please choose master worker" {exp_send "1\r"}
				#below expect was added for windows ssh service
		               -re     "Microsoft" {
				# for windows os
                           set os "windows"
                           while {$interrupt==0} {
                                   expect {
                                        "yes/no"   {exp_send "yes\n" }
                                        -re "Denying new connections" {
                                                log_info $info "Denying new connection" 2;
                                                return 2}
                                        -re "failure|Failed"  {
                                                puts "password error, cannot connect to DUT! "
                                                return 2}
                                        -re "login:" {exp_send "$USERNAME\r"}
                                        -re "Password:|password" {puts " password is : $PASSWORD"; exp_send "$PASSWORD\r" }
                                        -re "\\>" {puts "login to $DUT_HOSTNAME\n"; return 1}
                                        timeout {return 0}
                                   }
                           }
                		        }
				 -re    "\#|\(enable\)"      {
		                        puts "login to $DUT_HOSTNAME\n";
                		        return 1
		                        }
		                "Welcome"  {
        		                puts "welcome to $DUT_HOSTNAME\n";
#               		        return 1
                        		exp_continue
	                        	}
	        	        -re "Login incorrect|incorrect password"  {
        	        	        puts "password error, cannot connect to DUT! "
                	        	return 2}
	                	">"    {
		      	                exp_send "enable\r"
                		        expect  -re "Password:|password" {exp_send "$PASSWORD\n" }
					#fix bug for accessing cisco4006,Apr12_2006
					exp_continue
                        		}
		                -re "No route to host"  {
        		                puts "No route to host! "
                		        return 2}
	                	"Connection refused" {
	        	                puts "Connection refused! "
        	        	        return 2 }
			        "Connect failed" {
                		        puts "Connection refused! "
                        		return 2 }
#		               "Escape character" { exp_send "\r"}
        		        timeout    {return 0}
				}
			}
                "Welcome"  { 
			puts "welcome to $DUT_HOSTNAME\n"; 
#			return 1
			exp_continue
			}
                -re "Login incorrect|failure|Failed"  {
                        puts "password error, cannot connect to DUT! "
                        return 0}
		">"    { 
			exp_send "enable\r"
# when fix bug for rebooting device through minicom or terminal server, comment out below line
#			expect  -re "Password:|password" {exp_send "$PASSWORD\n" }
			}
                "Network is unreachable"  {
                        puts "Network is unreachable! "
                        return 2}
                "No route to host"  {
                        puts "No route to host! "
                        return 2}
                "Connection refused" {
                        puts "Connection refused! "
                        return 2 }
               "Connect failed" {
                        puts "Connection refused! "
                        return 2 }
#               "Escape character" { exp_send "\r"}
                timeout {
			if {$retry != 0 && $interrupt==0} {
				incr retry -1
				exp_send "\r"
			} else {
				return 0
			}
			}
           }
        }
    }

proc run_com_array_in_connection {com_array} {
        global SYS_RUN_PROMPT info SLEEP_RUN interrupt TIMEOUT get_special_exp
        #return value: 1 means run command array successfully and session still exist, need exit manually.
        #              2 means run command array successfully and session already disconnected, don't need exit manually.
        #              0 means run command array failure
        #       
        #Invoked by access_host_port and run_test
        array set command_array $com_array
        set timeout $TIMEOUT 
	set get_special_exp 1
        set expect_out(buffer) ""
	set prompt_string $SYS_RUN_PROMPT
	set special_timeout 0
        set newline "1"
        for {set i 0} {$i >= 0} {incr i +1} {
		set special_exp 1
		set next_i [expr $i+1]
		#check command exists, if not, return
		if {[info exists command_array($i)]==0} {return 1} 
		log_info $info "run command:$command_array($i)" 6
                if {$interrupt==1} {
                        log_info $info "system interrupted. in run_com_array_in_connection. " 2
                        return 1
                }
		#skip blank line
#		if {[string trimleft $command_array($i)]==""} {
#                        continue
#                }
		#strip leading and training blank
		set command_array($i) [string trim $command_array($i)]
		#handle comments
                if {[string range [string trimleft $command_array($i)] 0 2]=="###"} {
                        log_info $info "got comments:$command_array($i)" 5
                        continue
                }
                #handle special prompt command
                if {[regexp {prompt_test\s+(.*)} $command_array($i) nouse special_prompt]=="1" && [string range \
                [string trimleft $command_array($i)] 0 10]=="prompt_test"} {
                        log_info $info "got special prompt command, set special prompt: $special_prompt" 2
			if {$special_prompt=="reset" } {
				set prompt_string $SYS_RUN_PROMPT
			} else {
				if {[regexp {\{(.*)\}} $special_prompt nouse special_value]=="1" } {
					set prompt_string $special_value 
				} else {
					set prompt_string $special_prompt
				} 
			}
			continue
		}

                #handle special timeout command
                if {[regexp {timeout_test (.*)} $command_array($i) nouse exp_result]=="1" && [string range \
                [string trimleft $command_array($i)] 0 11]=="timeout_test"} {
			set special_timeout $exp_result
                        log_info $info "got timeout command, setup timeout value for the following command to $special_timeout seconds" 2
                        continue
                }

                #handle special newline command
                if {[regexp {newline_test (.*)} $command_array($i) nouse newline_value]=="1" && [string range \
                [string trimleft $command_array($i)] 0 11]=="newline_test"} {
                        set newline $newline_value
                        log_info $info "got newline command, setup newline value to $newline. 0 means no-newline, 1 means newline(default)" 2
                        continue
                }

		#handle special sleep command
                if {[regexp {sleep_test (.*)} $command_array($i) nouse sleeptime]=="1" && [string range \
		[string trimleft $command_array($i)] 0 9]=="sleep_test"} {
			log_info $info "got sleep command, sleep for $sleeptime seconds" 2
                        sleep $sleeptime
			continue
                }
		#analyze command to get exp_result		
		if {[regexp {(.*)\|\|\|(.*)} $command_array($i) nouse run_command exp_result]==0} {
#			set exp_result $SYS_RUN_PROMPT
			set exp_result $prompt_string
			set run_command $command_array($i)
			set special_exp 0
		} elseif {[regexp {(.*)\|\|\|(.*)} $run_command nouse run_command1 exp_result1]==1} {
			set special_timeout $exp_result
			set run_command $run_command1
			set exp_result $exp_result1
			log_info $info "specify timeout in command. command:$run_command. exp_result: $exp_result. timeout:$special_timeout" 4
		}
		if $newline=="1" {		
			log_info $info "run command(4):$command_array($i)" 6
			append run_command "\r"
			log_info $info "run command(5):$command_array($i)" 6
			log_info $info "newline is $newline, add \\r" 6
		} else {
			log_info $info "newline is $newline, do not add \\r" 3
                        set newline "1"
			log_info $info "reset newline to $newline, newline_test command can only take effective once" 3
		}

                set comm_res($i) [run_comm_exp_res $run_command $exp_result $special_timeout $prompt_string]
		log_info $info "command_result: $comm_res($i); special_exp: $special_exp; exp_result:$exp_result" 6
		if {$comm_res($i)==1} {
			#success, then will run follow command in the loop
			log_info $info "enter switch 1, special_exp: $special_exp;" 5 
			if {$special_exp==1} {
				log_info $info "cmd: $command_array($i)" 4
				log_info $info "got expect result in line!" 4
				#check if this is the last command, if it is, then do not need to expect prompt any more
				#set next_i [expr $i+1]
				if {[info exists command_array($next_i)]==0} {return 1} 
				log_info $info "next cmd: $command_array($next_i)" 4
			        if {$special_timeout!= 0} {
			                set timeout $special_timeout
			                log_info $info "using special_timeout:$special_timeout" 2
			        }
				expect {
					-re "--More--" {exp_send -raw "\x20"; exp_continue;}
#					-re $SYS_RUN_PROMPT { }
					-re $prompt_string { }
                			timeout {return 0 }
				}
			}
		} elseif {$comm_res($i)==0} {
			#command fail, exit loop and return 0
			log_info $info "enter switch 0, special_exp: $special_exp;" 5 
                	if {$special_exp==1} {
                                log_info $info "cmd: $command_array($i)" 4
                       		log_info $info "did not get expect result in line!" 3
				set get_special_exp 0
                	}
			#Add below line is because the default value is 1
			set get_special_exp 0
                	return 0;
		} else {
			#command fail/success and connection lost, exit loop and return 2
			log_info $info "enter switch 2, special_exp: $special_exp;" 5 
			if {$special_exp==1} {
				log_info $info "cmd: $command_array($i)" 4
				log_info $info "did not get expect result in line!" 3
				set get_special_exp 0
			}
			set get_special_exp 0
			log_info $info "command_result: $comm_res($i); return 2" 6; 
                        if {[info exists command_array($next_i)]==0} {
				return 2
			} else {
				log_info $info "session exist unexpectedly, command_array was not finished running" 2
				return 0
			}
		}
	}
}

proc run_comm_exp_res {comm exp_res special_timeout prompt} {
        global info SLEEP_RUN TIMEOUT TIMEOUT_IPSEC
	#For forticlient, when running ipsec tunnel_name command, it doesn't have any output, we only can wait for timeout, \
	then using a small timeout value
	log_info $info "special_timeout: $special_timeout" 5
	log_info $info "prompt: $prompt" 5
	if {[regexp "ipsec " $comm]==1} {
		set timeout $TIMEOUT_IPSEC
	} else {
		set timeout $TIMEOUT
	}
	if {$special_timeout!= 0} {
		set timeout $special_timeout
		log_info $info "using special_timeout:$special_timeout" 6
	} else {
		log_info $info "using normal timeout:$timeout" 6
        }

	log_info $info "cmd: $comm" 7
	log_info $info "exp_res: $exp_res" 7
	#Add "--" to force no option in comm
	if { [catch {exp_send -- $comm} err] != 0} {
		log_info $info "Unexpected error. $err" 2
		return 2
	}

        ###Special_timeout 10000 means password type inputing. then we do not need to expect the input string
        if {$special_timeout!=10000} {
                #Firstly expect input command to clear output buffer. the reason to expect only first 30 characters \
        is some applications will chop the string unexpectedly. 
                set cmd_30 [handle_string_special [string range $comm 0 30]]
                expect -- $cmd_30
                #expect -- [string range $comm 0 30]
        } else {
                log_info $info "special timeout 10000 means password type inputing, do not need to expect input command" 3
	}
	
	#for special character in expect string
	if {$exp_res!=$prompt} {
###Cannot remember why do we need below command. Now for test with regular expression, I have to remove it
	#	set exp_res [handle_string_special $exp_res]
	}
        expect {
		-re "do you want to delete child entries" {exp_send "yes\n"; exp_continue;}
		#for delete nat entries in ios
		-re "do you want to delete all entries?" {exp_send "yes\n"; exp_continue;}
		#for set vdom-admin disable
		-re "logged out for the operation to take effect" {
			exp_send "y";
                        expect "y";
                        log_info $info "exit from FG. Maybe by command 'set vdom-admin disable'" 3;                                 	
			set timeout $TIMEOUT;sleep 10;
			catch {exp_close} error; 
			catch {exp_wait} error;
			return 2;
			}
		#For command "restoring image or config",I add below command. but for "set port disable" in cisco switch, I comment out \
		below expect command, It seems we don't need expect something
		#"confirm" is for "clea line " command in cisco terminal server
                -re "\(y/n\)|confirm" {
			switch $comm {
                        	"exec factoryreset\r" {
                                	exp_send "y"
					expect "System is resetting"
                                        set SLEEP_RUN 120
                                        log_info $info "System is factoryreseting, for auto_run, system will wait for 120 seconds" 3
					sleep 2
		                        catch {exp_close}
                		        catch {exp_wait}
                       			return 1
                                        }
                                "exec formatlogdisk\r" {
                                        log_info $info "got formatlogdisk, for auto_run, system will wait for 120 seconds" 3
                                        exp_send "y"
                                        set SLEEP_RUN 120
                                        sleep 1
                                        catch {exp_close}
                                        catch {exp_wait}
                                        return 1
                                        }
                                "exec reboot\r" {
                                        exp_send "y"
					expect "rebooting"
                                        set SLEEP_RUN 120
                                        log_info $info "System is rebooting, for auto_run, system will wait for 120 seconds" 3
					sleep 2
		                        catch {exp_close}
                                        catch {exp_wait}
                                        return 1
                                        }
                                "purge\r" {
                                        exp_send "y"
                                        log_info $info "send out 'y' to purge command" 4
                                        exp_continue
                                        }
                                default {
                                        if {[regexp "exec restore image" $comm]==1} {
                                               set timeout 180
                                               exp_send "y"
                                               log_info $info "restoring image: just send out 'y' to command:$comm" 3;
                                               log_info $info "for auto_run, system will wait for 180 seconds" 3;
                                               expect {
							#For downgrading, system will ask confirmation again
	                                               -re "y/n" {exp_send "y";exp_continue;}
                                                       -re "restart" { 
                                               			log_info $info "got restart" 3;
								set timeout $TIMEOUT; 
								set SLEEP_RUN 120; sleep 10;
								#remove below line at Aug 24,2006. Found problem when upgrading from 280 to 300, \
								cannot upgrade it successfully. so removing below line to let system to exp_close the\
								 connection by itself
								#Add it again, if it's using console instead of telnet, without below line, \
								it won't exit.So, for restore image, should use console!
								catch {exp_close}; catch {exp_wait}; return 1
								}
                                                       -re "error|fail" {
                                               			log_info $info "got error or fail" 3;
								set timeout $TIMEOUT;
								log_info $info "Failed to upgrade image" 2; 
								return 0 }
						       -re "in progr|upgrade" {
                                               			log_info $info "got in progress" 3;
								set SLEEP_RUN 120;
								set timeout $TIMEOUT;sleep 10;
								catch {exp_close}; catch {exp_wait}; return 1
								}
						       #For upgrading from 280 to 300, need long time to import 280 configuration to 300
                                                       -re "importing|Importing" {
                                               			log_info $info "got importing" 3;
                                                                set SLEEP_RUN 240;
                                                                set timeout $TIMEOUT; sleep 10;
								catch {exp_close}; catch {exp_wait}; return 2
								}
							#For telnet connection, after confirmation, connection will be disconnected
                                                       -re "closed by foreign host" {
                                               			log_info $info "got closed by foreign host" 3;
								set timeout $TIMEOUT; 
								set SLEEP_RUN 120; 
								catch {exp_close}; catch {exp_wait}; return 2
								}
	                                               }
                                        } elseif {[regexp "exec restore config" $comm]==1} {
                                               set timeout 60
                                               exp_send "y"
                                               log_info $info "restoring config: just send out 'y' to command:$comm" 3;
                                               log_info $info "for auto_run, system will wait for 120 seconds" 3;
                                               expect {
                                                       -re "Invalid|error|fail|Can\ not\ get\ config\ file" {
								set timeout $TIMEOUT; 
								log_info $info "Failed to load config file" 2; 
								return 0 }
                                                      -re "restart|File\ check\ OK" {
                                                                set timeout $TIMEOUT;
                                                                set SLEEP_RUN 120;
								sleep 10
                                                                catch {exp_close};
                                                                catch {exp_wait};
                                                                return 2}
							#For telnet connection, after confirmation, connection will be disconnected
                                                       -re "closed by foreign host" {
                                                                set timeout $TIMEOUT;
                                                                set SLEEP_RUN 120;
                                                                catch {exp_close};
                                                                catch {exp_wait};
                                                                return 2}
                                                       }
                                        } elseif {[regexp "exec restore av" $comm]==1} {
                                               set timeout 200
                                               exp_send "y"
                                               log_info $info "restoring av package: just send out 'y' to command:$comm" 3;
                                               log_info $info "for auto_run, system will wait for 200 seconds" 3;
                                               expect {
                                                        -re "Invalid|error|fail|Can\ not\ get\ config\ file" {
                                                                set timeout $TIMEOUT;
                                                                log_info $info "Failed to load av package file" 2;
								return 0}
							-re "OK" {
								log_info $info "Load AV package successfully" 2;
								}
							}
						exp_continue;
                                        } elseif {[regexp "exec log delete" $comm]==1} {
                                               exp_send "y"
                                               log_info $info "delete log,just send out 'y' to command:$comm" 4;
                                               exp_continue
                                        } elseif {[regexp "clea line|clear line" $comm]==1} {
                                               exp_send "y"
                                               log_info $info "clea line,just send out 'y' to command:$comm" 4;
                                               exp_continue
                                        } else {
                                               exp_send "y\r"
                                               log_info $info "default: just send out 'y\\r' to command:$comm" 4;
                                               exp_continue
                                        }
                                        }
                        }
         	        }

                -re "$exp_res" {
			#If get result, don't need to expect SYS_PROMPT any more, so I comment out below expect line
			#                       exp_send "\x03"; 
set exp_index 1
while {1==[info exists expect_out($exp_index,string)]} {
set b1 [info exists expect_out($exp_index,string]
log_info $info "b1:$b1" 2
set a($exp_index) $expect_out($exp_index,string)
log_info $info "a($exp_index) is $a($exp_index)" 2
incr exp_index +1
}
set exp_index 1
while {1==[info exists a($exp_index)]} {
log_info $info "a($exp_index) is $a($exp_index)" 2
incr exp_index +1
}

			return 1;
                        }
                -re $prompt {
			#because session table include ">" character, so I removed ">" from SYS_PROMPT match
			#                -re "\\$|\#|\(enable\)" 
                        return 0
                        }
		-re "--More--" {exp_send -raw "\x20"; exp_continue;}
                -re "closed by foreign host" {
                	log_info $info "closed by foreign host, maybe by 'diag sys sess clear'" 4;
                        catch {exp_close}
                        catch {exp_wait}
                        return 2
                 	}
		timeout {
			# send a Ctrl+C in case the command cannot end by itself
			#comment out this Ctrl_C for enabling Forticlient test. when run IPSEC in windows, it won't exit by itself.\
			 we hope it was left there
			#                       exp_send  "\x03"
                        return 0
                        }
        }

}

proc access_host_port {access_host access_mode access_user access_pass access_port com_array exit_process} {
	#return 0 means fail, otherwise return spawn_id. 
	#If exit_process is 1 then connection already was closed in this proc, otherwise it should be closed by upper layer by returning spawn_id.	
	global SYS_RUN_PROMPT info SLEEP_RUN spawn_id
	set expect_out(buffer) ""
        set retry_num 3	
	set spawn_id [connect_setup  $access_host $access_user $access_pass $access_mode $access_port]
	if {$spawn_id!=0} {
		if {[set run_result [run_com_array_in_connection $com_array]]!=2} {
			if {$exit_process==1} {
				log_info $info "exit immediatelly" 5
				exit_from_dev $access_port $access_mode $spawn_id
				if {$run_result==0} {
					return $run_result
				} else {
					return $spawn_id
				}								
			} else {
			#for exit_process is 0, donot care fail or success, just return spawn_id to upper layer				
				return $spawn_id
			}
		} elseif {$access_mode=="telnet"&&$access_port=="23" || $access_mode=="ssh"} {
			log_info $info "Do not need exit because session already exited" 2
			return $spawn_id
		} else {
			#for minicom or terminal server mode, we still need exit manually. edit at Apr20,2006
			#Changed at May 1st,2006, it seems we don't need exit manually for all cases because we already exit in run_com_array_in_connection
			log_info $info "exit from minicom or terminal server session" 2
			#exit_from_dev $access_port $access_mode $spawn_id
			return $spawn_id
		}
	} else {
		return 0
	}
}

proc ch_string_to_array_new {comm_string comm_array1} {
        global info
        upvar $comm_array1 comm_array
        catch {unset comm_array}
	set comm_array(1) ""
        set array_string [split $comm_string "\n"]
        set line_number [llength $array_string]
        set str0 [lindex $array_string 0]
        for {set i 0} {$i<$line_number} {incr i +1} {
                set comm_array($i) [lindex $array_string $i]
                log_info $info "$i: $comm_array($i)" 6
        }
}

###proc ch_string_to_array {comm_string } {
###	global info comm_array
###	catch {unset comm_array}
###	set array_string [split $comm_string "\n"]
###	set line_number [llength $array_string]
###	set str0 [lindex $array_string 0]
###	for {set i 0} {$i<$line_number} {incr i +1} {
###        	set comm_array($i) [lindex $array_string $i]
###		log_info $info "$i: $comm_array($i)" 7
###	}
###}

###proc run_comm_exp_res_old {comm exp_res} {
###	global SYS_PROMPT info SLEEP_RUN
###	set timeout 16
###	exp_send $comm
####for special character in expect string
###	set exp_res [string map {\" \\" \' \\' \( \\\( \) \\\) \[ \\\[ \] \\\]} $exp_res]
###	expect {
####For command "restoring image or config",I add below command. but for "set port disable" in cisco switch, I comment out below expect command, It seems we don't need expect something
###		-re "y/n" {
###                                switch $comm {
###                                        "exec factoryreset" {
###                                                exp_send "y"
###                                                set SLEEP_RUN 120
###                                                log_info $info "System is factoryreseting, for auto_run, system will wait for 120 seconds" 3
###                                                }
###                                        "exec reboot" {
###                                                exp_send "y"
###                                                set SLEEP_RUN 120
###                                                log_info $info "System is rebooting, for auto_run, system will wait for 120 seconds" 3
###                                                }
###                                        "purge" {
###                                                exp_send "y"
###                                                log_info $info "send out 'y' to purge command" 3
###						exp_continue
###                                                }
###                                        default {
###                                                exp_send "y/r"
###                                                log_info $info "just send out 'y' to command:$command_array($i)" 3;
###						exp_continue
###                                                }
###                                }
###                        }
###                        
###		-re "$exp_res" { 
####If get result, don't need to expect SYS_PROMPT any more, so I comment out below expect line
###			exp_send "\x03"; return 1;
###			}
####		-re $SYS_PROMPT  
####because session table include ">" character, so I removed ">" from SYS_PROMPT match
###		-re "\\$|\#|\(enable\)" { 
###			return 0
###			}
###		timeout {
#### send a Ctrl+C in case the command cannot end by itself
###			exp_send  "\x03" 
###			return 0
###			}
###	}
###
###}
###proc run_comm_exp_res_2 {comm exp_res} {
###        global SYS_PROMPT
###        set timeout 16
###        exp_send $comm
###        expect {
###                -re $exp_res { puts "got it once!"; expect -re $exp_res ; exp_send "q" ; return 1}
####                -re $SYS_PROMPT {
####                       sleep 10;
####                       puts "Error, do not get result. hahaha" ;
####                        exp_send "q"; return 0
####                        }
###                timeout { exp_send "q"; return 0}
###        }
###
###}
